layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };

    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {field: 'pid', align: "center", hide: true, sort: true, title: 'pid'},
            {field: 'csqy', align: "center", sort: true, title: '所属区域', width: 130},
            {field: 'yyjlqy', align: "center", sort: true, title: '营运经理区域', width: 130},
            {field: 'dd', align: "center", sort: true, title: '督导', width: 130},
            {field: 'ctbh', align: "center", sort: true, title: '餐厅编号', width: 130},
            {field: 'dm', align: "center", sort: true, title: '店名'},
            {field: 'kysj', align: "center", sort: true, title: '开业时间', width: 130},
            {field: 'ctdh', align: "center", sort: true, title: '餐厅电话', width: 130},
            {field: 'ctdz', align: "center", sort: true, title: '餐厅地址', width: 393},
            {field: 'fzr', align: "center", sort: false, title: '店长', width: 80},
            {field: 'lxfs', align: "center", sort: false, title: '联系方式', width: 120},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['ctbh'] = $("#ctbh").val();
        queryData['dm'] = $("#dm").val();
        queryData['csqy'] = $("#csqy").val();
        queryData['yyjl'] = $("#yyjlqy").val();
        queryData['dd'] = $("#dd").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击导出按钮
     */
    Notice.export = function () {
        var queryData = {};
        queryData['ctbh'] = $("#ctbh").val();
        queryData['dm'] = $("#dm").val();
        queryData['csqy'] = $("#csqy").val();
        var ajax = new $ax("/btyTxl/export", function (data) {
        }, function (data) {
        });
        ajax.set("queryData", queryData);
        const result = ajax.start();
        if (result.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, result, 'xls');
        }
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 导出通讯录
    $('#btnExport').click(function () {
        Notice.export();
    });

    /**
     * 弹出添加餐厅通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 800,
            title: '添加餐厅',
            content: Feng.ctxPath + '/btyTxl/btyTxl_addct',
            tableId: Notice.tableId
        });
    };
    /**
     * 弹出添加负责人通知
     */
    Notice.openAddNotice2 = function () {
        func.open({
            height: 500,
            title: '添加负责人',
            content: Feng.ctxPath + '/btyTxl/btyTxl_addfzr',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        debugger
        var operation = function () {
            var id = data.id;
            var ajax = new $ax(Feng.ctxPath + "/btyTxl/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除 ?", operation);
    };
    /**
     * 点击查看明细数据
     */
    Notice.onViewNotice = function (data) {
        func.open({
            height: 980,
            title: '负责人明细',
            content: Feng.ctxPath + "/btyTxl/btyTxl_view/" + data.id,
            tableId: Notice.tableId
        });
    };
    /**
     * 点击修改
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {


        func.open({
            height: 980,
            title: '修改',
            content: Feng.ctxPath + "/btyTxl/btyTxl_update/" + data.id,
            tableId: Notice.tableId
        });

    };
    // 渲染表格
    var tableResult = table.render({
        elem: "#" + Notice.tableId,
        url: Feng.ctxPath + '/btyTxl/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        limit: 20,
        cols: Notice.initColumn()
    });


    // 添加餐厅
    $('#btnAddct').click(function () {
        Notice.openAddNotice();
    });
    //添加负责人
    $('#btnAddfzr').click(function () {
        Notice.openAddNotice2();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        } else if (layEvent === 'view') {
            Notice.onViewNotice(data);
        }
    });
});